package com.study.jvm.classloader;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by tan on 2017/6/21.
 */
public class CloaderLoaderTest {
	public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
		ClassLoader myloder = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException {
				try {
					String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
					InputStream is = getClass().getResourceAsStream(fileName);
					if (is == null) {
						return super.loadClass(name);
					}
					byte[] b = new byte[is.available()];
					is.read(b);
					return defineClass(name, b, 0, b.length);
				} catch (IOException e) {
					throw new ClassNotFoundException(name);
				}
			}
		};


		Class clazz = null;
		clazz.newInstance();
		clazz.equals(null);
		Object object = myloder.loadClass("com.study.jvm.classloader.CloaderLoaderTest").newInstance();
		System.out.println(object.getClass());
		System.out.println(object.getClass().getClassLoader());
		System.out.println(com.study.jvm.classloader.CloaderLoaderTest.class.getClassLoader());

		Object person = myloder.loadClass("com.study.jvm.classloader.Person").newInstance();
		System.out.println(person.getClass());
		System.out.println(person.getClass().getClassLoader());
		System.out.println(com.study.jvm.classloader.Person.class.getClassLoader());
		//instanceof 只能 系统加载的类吗
		System.out.println(object instanceof com.study.jvm.classloader.CloaderLoaderTest);
		System.out.println(person instanceof com.study.jvm.classloader.Person);
	}
}
